package com.westar.base.businessUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.westar.core.conf.SysConst;
import com.westar.base.exceptions.LoginException;
import com.westar.base.model.Area;
import com.westar.base.util.WestarHttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AreaPageUtil {


    /**
     * 取得所有区域数据信息
     * @param actCookie
     * @return
     * @throws IOException
     * @throws LoginException
     */
    public static List<Area> queryAllArea(String actCookie) throws IOException {
        List<Area> areaList = new ArrayList<>();

        String url = "http://"+ SysConst.DOMAIN+"/oauthui/area/listData";

        Map<String,String> searchForm = new HashMap<>( );
        searchForm.put( "page","1" );
        searchForm.put( "rows","10" );

        String responeBody = WestarHttpUtil.postMethod( url,actCookie,searchForm);
        JSONObject res = JSONObject.parseObject(responeBody);
        Integer total = res.getInteger("total");
        if(total > 0){
            List<Area> areaSubList = AreaPageUtil.getAreaFromPage(res,url,actCookie);
            areaList.addAll(areaSubList);
            Integer totalPageNum = total/10 + 1;
            for(int i=2;i<=totalPageNum;i++){
                searchForm.put( "page",i+"" );
                searchForm.put( "rows","10" );
                responeBody = WestarHttpUtil.postMethod( url,actCookie,searchForm);
                res = JSONObject.parseObject(responeBody);

                areaSubList = AreaPageUtil.getAreaFromPage(res,url,actCookie);
                areaList.addAll(areaSubList);
            }

        }
        return areaList;



    }

    private static List<Area> getAreaFromPage(JSONObject res, String url, String actCookie)
            throws IOException {
        List<Area> areaList = new ArrayList<>();
        JSONArray rows = JSONArray.parseArray( res.getString( "rows" ));
        if(null != rows && !rows.isEmpty()){
            for (Object data :rows){
                JSONObject areaJson = JSONObject.parseObject(data.toString());
                Area area = new Area();

                area.setAreaId(areaJson.getString( "idForStr" ));
                area.setParentAreaId(areaJson.getLong( "parentAreaId" ));
                area.setAreaCode(areaJson.getString( "areaCode"));
                area.setAreaName(areaJson.getString( "areaName"));
                area.setAreaSimpleName(areaJson.getString( "areaSimpleName"));
                area.setUpdateTime(areaJson.getString( "updateTime"));

                areaList.add(area);

                String state = areaJson.getString( "state");
                if(state.equals( "closed" )){
                    List<Area> areaSubList = AreaPageUtil.getAreaSubFromPage(url,actCookie,area.getAreaId());
                    areaList.addAll(areaSubList);
                }
            }
        }


        return areaList;

    }

    private static List<Area> getAreaSubFromPage(String url, String actCookie, String parentId)
            throws IOException {
        List<Area> areaList = new ArrayList<>();
        Map<String,String> searchForm = new HashMap<>( );
        searchForm.put( "page","1" );
        searchForm.put( "rows","10" );
        searchForm.put( "id",parentId.toString() );

        String responeBody = WestarHttpUtil.postMethod( url,actCookie,searchForm);
        JSONArray rows = JSONArray.parseArray( responeBody);
        if(null != rows && !rows.isEmpty()){
            for (Object data :rows){
                JSONObject areaJson = JSONObject.parseObject(data.toString());
                Area area = new Area();

                area.setAreaId(areaJson.getString( "idForStr" )  );
                area.setParentAreaId(areaJson.getLong( "parentAreaId" ));
                area.setAreaCode(areaJson.getString( "areaCode"));
                area.setAreaName(areaJson.getString( "areaName"));
                area.setAreaSimpleName(areaJson.getString( "areaSimpleName"));
                area.setUpdateTime(areaJson.getString( "updateTime"));

                areaList.add(area);

                String state = areaJson.getString( "state");
                if(state.equals( "closed" )){
                    List<Area> areaSubList = AreaPageUtil.getAreaSubFromPage(url,actCookie,area.getAreaId());
                    areaList.addAll(areaSubList);
                }

            }
        }
        return areaList;
    }
}
